package com.tbs.easyhomemoneyserver.auths

import com.tbs.easyhomemoneyserver.DO.ApiLimit
import com.tbs.easyhomemoneyserver.constants.ApiRightsConstants.SU_TOKEN_HEADER
import com.tbs.easyhomemoneyserver.constants.BeanNameConstants.SU_USER_AUTH
import com.tbs.easyhomemoneyserver.interfaces.IApplicationSetting
import com.tbs.easyhomemoneyserver.interfaces.IRightAuthorization
import com.tbs.easyhomemoneyserver.model.rights.ApiRequest
import org.apache.dubbo.config.annotation.DubboReference
import org.apache.dubbo.config.annotation.DubboService
import org.apache.dubbo.rpc.RpcContext
import javax.servlet.http.HttpServletRequest

@DubboService(group = SU_USER_AUTH)
class SuUserAuth : IRightAuthorization {


    @DubboReference(lazy = true)
    lateinit var appSetting: IApplicationSetting

    override fun auth(limit: ApiLimit, apiRequest: ApiRequest): Boolean {
        val request: HttpServletRequest = RpcContext.getContext().getRequest(HttpServletRequest::class.java)
        val src = request.getHeader(SU_TOKEN_HEADER) //        return setting!!.suToken == src

        return appSetting.getSetting().SuToken == src
    }

    override fun unAuthedText(request: ApiRequest, noPassRights: ApiLimit): String {
        return "Su Token Wrong"
    }
}
